﻿import GameConfig from "./GameConfig";



import ScreenPositionUtil from "./ScreenPositionUtil";

var g_main = null;
var gCadSrcSmallImg = "poics/scratch_front_src_small.png";

var gSetSrcImg = function (e) {
	gCadSrcSmallImg = e;
	g_main.setSrcImage(e);
}

var gResetScene = function () {
	g_main.resetSceneState(e);
}

class Main {
	constructor() {
		this.onCreateCommand(GameConfig.width,GameConfig.height ,1);
	}



	onCreateCommand(inW, inH, inPr) {
		if(Laya.stage != null)
			return;

		this.cadBgImg = "poics/scratch_bg.png";
		this.cadBottomImg = "poics/scratch_bottom.png";
		this.cadBottomGrayImg = "poics/scratch_front_bg.png"
		this.cadSrcImg = "poics/scratch_front_src.png"
		this.cadSrcSmallImg = "poics/scratch_front_src_small.png"

		GameConfig.width = inW * inPr;
		GameConfig.height = inH * inPr;
		Laya.Config.isAntialias = true;
		//根据IDE设置初始化引擎		
		if (window["Laya3D"]) Laya3D.init(GameConfig.width, GameConfig.height);
		else Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
		Laya["Physics"] && Laya["Physics"].enable();
		Laya["DebugPanel"] && Laya["DebugPanel"].enable();
		Laya.stage.scaleMode = GameConfig.scaleMode;
		Laya.stage.screenMode = GameConfig.screenMode;
		Laya.stage.alignV = GameConfig.alignV;
		Laya.stage.alignH = GameConfig.alignH;
		//兼容微信不支持加载scene后缀场景
		Laya.URL.exportSceneToJson = GameConfig.exportSceneToJson;

		//打开调试面板（通过IDE设置调试模式，或者url地址增加debug=true参数，均可打开调试面板）
		if (GameConfig.debug || Laya.Utils.getQueryString("debug") == "true") Laya.enableDebugPanel();
		if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"]) Laya["PhysicsDebugDraw"].enable();
		if (GameConfig.stat) Laya.Stat.show();
		Laya.alertGlobalError(true);

		//激活资源版本控制，version.json由IDE发布功能自动生成，如果没有也不影响后续流程
		Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);


		let loadList = ["https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/EICO_JORDAN/scene/scratchcad/poics.atlas"]
		Laya.loader.load(loadList, Laya.Handler.create(this, this.initScene));
	}

	onVersionLoaded() {
		//激活大小图映射，加载小图的时候，如果发现小图在大图合集里面，则优先加载大图合集，而不是小图
		Laya.AtlasInfoManager.enable("fileconfig.json", Laya.Handler.create(this, this.onConfigLoaded));
	}

	onConfigLoaded() {
		//加载IDE指定的场景
		GameConfig.startScene && Laya.Scene.open(GameConfig.startScene);
	}


	initScene() {
		debugger
		ScreenPositionUtil.setStageWidthAndHeight(Laya.stage.width, Laya.stage.height);
		Laya.stage.bgColor = "#00000055";

		this.parentNode = new Laya.Sprite;
		Laya.stage.addChild(this.parentNode);
		this.parentNode.pos(Laya.stage.width / 2, Laya.stage.height / 2);
		this.CAD_MARGIN_LEFT_RIGHT = 0.1;
		this.minTouchX = 999;
		this.maxTouchX = 0;
		
		this.bgImgRatio = 1;
		this.srcBottomRatio = 1.93;
		this.canTouch = true;

		// 底图框
		this.bgImgView = new Laya.Sprite;
		this.parentNode.addChild(this.bgImgView);
		this.bgImgView.size(
			Laya.stage.width - (ScreenPositionUtil.getPostionInStage(this.CAD_MARGIN_LEFT_RIGHT, "x") * 2),
			(Laya.stage.width - (ScreenPositionUtil.getPostionInStage(this.CAD_MARGIN_LEFT_RIGHT, "x") * 2)) * this.bgImgRatio);
		this.bgImgView.pivot(this.bgImgView.width / 2, this.bgImgView.height / 2);
		this.bgImgView.loadImage(this.cadBgImg);

		// 粉色底图
		this.srcBottomImgView = new Laya.Sprite;
		this.srcBottomImgView.size( this.bgImgView.width * 0.75, this.bgImgView.width * 0.75  / this.srcBottomRatio );
		this.srcBottomImgView.pivot(this.srcBottomImgView.width / 2, this.srcBottomImgView.height / 2);
		this.srcBottomImgView.pos(0, this.bgImgView.height * 0.1);
		this.srcBottomImgView.loadImage(this.cadBottomImg);
		this.parentNode.addChild(this.srcBottomImgView);

		// 灰色底图
		this.srcImgView = new Laya.Sprite;
		this.parentNode.addChild(this.srcImgView);
		this.srcImgView.size( this.bgImgView.width * 0.74, this.bgImgView.width * 0.74  / this.srcBottomRatio );
		this.srcImgView.pivot(this.srcImgView.width / 2, this.srcImgView.height / 2);
		this.srcImgView.pos(0, this.bgImgView.height * 0.1);
		this.srcImgView.loadImage(this.cadBottomGrayImg);

		// 商品图
		this.srcGoodView = new Laya.Sprite;
		this.srcImgView.addChild(this.srcGoodView);
		this.srcGoodView.size( this.srcImgView.height * 0.8, this.srcImgView.height * 0.8);
		this.srcGoodView.pivot(this.srcGoodView.width / 2, this.srcGoodView.height / 2);
		this.srcGoodView.pos(this.srcImgView.width / 2 , this.srcImgView.height / 2);
		this.srcGoodView.loadImage(this.cadSrcSmallImg);

		// 遮罩
		this.srcMask = new Laya.Sprite;
		this.srcMask.size(this.srcImgView.width, this.srcImgView.height);
		this.srcImgView.mask = this.srcMask;

		this.srcImgView.on(Laya.Event.MOUSE_DOWN, this, (event) => {
			if (!this.canTouch)
				return;
			this.isTouched = true;
		});
		this.srcImgView.on(Laya.Event.MOUSE_MOVE, this, (event) => {
			if (!this.canTouch)
				return;
			if (this.isTouched) {
				let tempPoint = this.srcImgView.globalToLocal(new Laya.Point(event.stageX, event.stageY));
				this.srcMask.graphics.drawCircle(tempPoint.x, tempPoint.y, 20, "red");

				if (tempPoint.x < this.minTouchX) {
					this.minTouchX = tempPoint.x;
				}
				if (tempPoint.x > this.maxTouchX) {
					this.maxTouchX = tempPoint.x;
				}

				if (this.maxTouchX - this.minTouchX > (Laya.stage.width / 2)) {
					console.log("finish");
					this.showAllScene();
				}
			}
		});
		this.srcImgView.on(Laya.Event.MOUSE_UP, this, (event) => {
			this.isTouched = false;
		});
	}

	showAllScene() {
		this.canTouch = false;
		this.srcImgView.mask = null;
	}

	resetSceneState() {
		this.canTouch = true;
		this.srcImgView.mask = null;
		this.srcMask = new Laya.Sprite;
		this.srcMask.size(this.srcImgView.width, this.srcImgView.height);
		this.srcImgView.mask = this.srcMask;
		this.minTouchX = 999;
        this.maxTouchX = 0;
	}

	setSrcImage(inSrc) {
		gCadSrcSmallImg = inSrc;
		this.srcGoodView.loadImage(gCadSrcSmallImg);
	}
}
//激活启动类
g_main = new Main();
